package org.example;

import java.util.Stack;

public class Test5 {
    //leetcode 150 逆波兰表达式 https://leetcode.cn/problems/evaluate-reverse-polish-notation/description/
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        int num1, num2;
        for (String s : tokens) {
            if (s.equals("+")) {
                num2 = stack.pop();
                num1 = stack.pop();
                stack.add(num1 + num2);
            }else if (s.equals("-")) {
                num2 = stack.pop();
                num1 = stack.pop();
                stack.add(num1 - num2);
            }else if (s.equals("*")) {
                num2 = stack.pop();
                num1 = stack.pop();
                stack.add(num1 * num2);
            }else if (s.equals("/")) {
                num2 = stack.pop();
                num1 = stack.pop();
                stack.add(num1 / num2);
            }else {
                stack.add(Integer.parseInt(s));
            }
        }
        return stack.pop();
    }
}
